<?php
/**
 * @version		$Id: media.php 14401 2010-01-26 14:10:00Z louis $
 * @package		Joomla
 * @subpackage	Media
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

//define a maxim size for the uploaded images in Kb
 define ("MAX_SIZE","1000"); 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
 $errors=0;
//checks if the form has been submitted
 if(isset($_POST['Submit'])) 
 {
 	//reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	//if it is not empty
 	if ($image) 
 	{
 	//get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['image']['name']);
 	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 	//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
	//otherwise we will do more tests
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
		//print error message
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server
 $size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
	echo '<h1>You have exceeded the size limit!</h1>';
	$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=$_POST['name'].'.'.$extension;
	
//the new name will be containing the full path where will be stored (images folder)
$newname="images/frontpage/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
	echo '<h1>Copy unsuccessfull!</h1>';
	$errors=1;
}
   include(JPATH_COMPONENT_ADMINISTRATOR.DS.'classresize.php');
   $image = new SimpleImage();
   $image->load($newname);
   $image->resize(231,197);
   $image->save($newname);
}}}
$db=JFactory::getDBO();
//If no errors registred, print the success message
 if(isset($_POST['Submit']) && !$errors) 
 {
	$db->setQuery('update #__images_frontpage set title="'.str_replace("'", " ",$_POST['title']).'",link="'.$_POST['link'].'" where id='.$_POST['id']);
	if($db->query())
 	echo "<h1>File Uploaded Successfully! </h1>";
	else
	echo "<h1>Update title unsuccessfull! </h1>";
 }

$db->setQuery('select * from #__images_frontpage order by id');
$rows=$db->loadObjectList();
 ?>
<script language="javascript">
	function setTitle(title,name,link)
	{
		document.uploadImageForm.title.value=title;
		document.uploadImageForm.name.value=name;
		document.uploadImageForm.link.value=link;
		return;
	}
</script>
 <!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" -->
 <form name="uploadImageForm" method="post" enctype="multipart/form-data"  action="">
 <table>
	<tr>
		<td  width="231" height="197" align="center"style="border:1px solid red;" ><h2>231px x 197px</h2></td>
		<td width="230" height="197" align="center" style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[0]->url ?>"/></td>
		<td width="231" height="197" align="center" style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[2]->url ?>"/></td>
	</tr>
	<tr>
	<td></td>
		<td width="230" align="center" ><input checked type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[0]->title) ?>","kader-blauw","<?php echo $rows[0]->link ?>")' value="<?php echo $rows[0]->id ?>" />1</td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[2]->title) ?>","kader_valentijn","<?php echo $rows[2]->link ?>")' value="<?php echo $rows[2]->id ?>" />2</td>
	</tr>
	<tr>
		<td width="231" height="197" align="center"style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[4]->url ?>"/></td>
		<td width="231" height="197" align="center"style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[6]->url ?>"/></td> 
		<td width="231" height="197" align="center"style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[8]->url ?>"/></td>
	</tr>
	<tr>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[4]->title) ?>","kader-magenta","<?php echo $rows[4]->link ?>")' value="<?php echo $rows[4]->id ?>" />3</td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[6]->title) ?>","kader-oranje","<?php echo $rows[6]->link ?>")' value="<?php echo $rows[6]->id ?>" />4</td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[8]->title) ?>","kader-wit","<?php echo $rows[8]->link ?>")' value="<?php echo $rows[8]->id ?>" />5</td>
	</tr>
 </table>
  <table>
	<tr><td colspan="2"><input type="file" name="image"></td></tr>
	<tr><td>Title</td><td><input type="text" name="title" style="width:195px;" value=<?php echo $rows[0]->title ?>></td></tr>
	<tr><td>Link to</td><td><input type="text" name="link" style="width:195px;" value=<?php echo $rows[0]->link ?>></td></tr>
 	<tr><td colspan="2"><input name="Submit" type="submit" value="Save & Upload image"></td></tr>
 </table>
 <table>
	<tr><td colspan="3"><h4>Shadow</h4></td></tr>
	<tr>
		<td  width="231" height="197" align="center"style="border:1px solid red;" ><h2>231px x 197px</h2></td>
		<td width="230" height="197" align="center" style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[1]->url ?>"/></td>
		<td width="231" height="197" align="center" style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[3]->url ?>"/></td>
	</tr>
	<tr>
	<td></td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[1]->title) ?>","kader-blauwmo","<?php echo $rows[1]->link ?>")' value="<?php echo $rows[1]->id ?>" />1</td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[3]->title) ?>","kader_valentijnmo","<?php echo $rows[3]->link ?>")' value="<?php echo $rows[3]->id ?>" />2</td>
	</tr>
	<tr>
		<td width="231" height="197" align="center"style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[5]->url ?>"/></td>
		<td width="231" height="197" align="center"style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[7]->url ?>"/></td> 
		<td width="231" height="197" align="center"style="border:1px solid red;"><image width="100%" height="100%" src="<?php echo $rows[9]->url ?>"/></td>
	</tr>
	<tr>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[5]->title) ?>","kader-magentamo","<?php echo $rows[5]->link ?>")' value="<?php echo $rows[5]->id ?>" />3</td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[7]->title) ?>","kader-oranjemo","<?php echo $rows[7]->link ?>")' value="<?php echo $rows[7]->id ?>" />4</td>
		<td width="230" align="center" ><input type="radio" name="id" onclick='setTitle("<?php echo addslashes($rows[9]->title) ?>","kader-witmo","<?php echo $rows[9]->link ?>")' value="<?php echo $rows[9]->id ?>" />5</td>
	</tr>
 </table>
	<input type="hidden" name="name" value="kader-blauw" />
 </form>